package com.shanxi.cz.module.biz.controller.admin.ammeterrecordyear;

import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.shanxi.cz.framework.common.pojo.PageParam;
import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.pojo.CommonResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import static com.shanxi.cz.framework.common.pojo.CommonResult.success;

import com.shanxi.cz.framework.excel.core.util.ExcelUtils;

import com.shanxi.cz.framework.apilog.core.annotation.ApiAccessLog;
import static com.shanxi.cz.framework.apilog.core.enums.OperateTypeEnum.*;
import static com.shanxi.cz.module.biz.enums.LogRecordConstants.*;

import com.shanxi.cz.module.biz.controller.admin.ammeterrecordyear.vo.*;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecordyear.AmmeterRecordYearDO;
import com.shanxi.cz.module.biz.service.ammeterrecordyear.AmmeterRecordYearService;

@Tag(name = "管理后台 - 电表数据年报")
@RestController
@RequestMapping("/biz/ammeter-record-year")
@Validated
public class AmmeterRecordYearController {

    @Resource
    private AmmeterRecordYearService ammeterRecordYearService;

    @PostMapping("/create")
    @Operation(summary = "创建电表数据年报")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-year:create')")
    public CommonResult<Integer> createAmmeterRecordYear(@Valid @RequestBody AmmeterRecordYearSaveReqVO createReqVO) {
        return success(ammeterRecordYearService.createAmmeterRecordYear(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电表数据年报")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-year:update')")
    public CommonResult<Boolean> updateAmmeterRecordYear(@Valid @RequestBody AmmeterRecordYearSaveReqVO updateReqVO) {
        ammeterRecordYearService.updateAmmeterRecordYear(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电表数据年报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-year:delete')")
    public CommonResult<Boolean> deleteAmmeterRecordYear(@RequestParam("id") Integer id) {
        ammeterRecordYearService.deleteAmmeterRecordYear(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除电表数据年报")
                @PreAuthorize("@ss.hasPermission('biz:ammeter-record-year:delete')")
    public CommonResult<Boolean> deleteAmmeterRecordYearList(@RequestParam("ids") List<Integer> ids) {
        ammeterRecordYearService.deleteAmmeterRecordYearListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电表数据年报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-year:query')")
    public CommonResult<AmmeterRecordYearRespVO> getAmmeterRecordYear(@RequestParam("id") Integer id) {
        AmmeterRecordYearDO ammeterRecordYear = ammeterRecordYearService.getAmmeterRecordYear(id);
        return success(BeanUtils.toBean(ammeterRecordYear, AmmeterRecordYearRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电表数据年报分页")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-year:query')")
    public CommonResult<PageResult<AmmeterRecordYearRespVO>> getAmmeterRecordYearPage(@Valid AmmeterRecordYearPageReqVO pageReqVO) {
        PageResult<AmmeterRecordYearDO> pageResult = ammeterRecordYearService.getAmmeterRecordYearPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AmmeterRecordYearRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电表数据年报 Excel")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-record-year:export')")
    @ApiAccessLog(operateType = EXPORT)
    @LogRecord(type = SYSTEM_USER_TYPE, subType = EXPORT_YEAR_REPORT_TYPE, bizNo = "{{#pageReqVO.ammeterId}}",
            success = EXPORT_YEAR_REPORT_SUCCESS)
    public void exportAmmeterRecordYearExcel(@Valid AmmeterRecordYearPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AmmeterRecordYearDO> list = ammeterRecordYearService.getAmmeterRecordYearPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电表数据年报.xls", "数据", AmmeterRecordYearRespVO.class,
                        BeanUtils.toBean(list, AmmeterRecordYearRespVO.class));
    }

}